
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.Vector;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Laurens
 */
public class replaceValue 
{
    characterList characters = new characterList();
    NumberList numbers = new NumberList();
    public replaceValue()
    {
        
    }
    
    /**
     * This function replaces the oldValue with the newValue, where the line numbers correspond to lineNumbers<String>
     * and is situated in the text field file
     * @param lineNumbers, line numbers where oldValue is found
     * @param oldValue, Method to be changed
     * @param newValue, new Method to be inserted
     * @param file, holds the file currently working with
     * @throws IOException
     */
    public String replace(Vector<Integer> lineNumbers, String oldValue, String newValue, String file, boolean method) throws IOException
    {
        Vector<Integer> sortedLineNumbers = new Vector<Integer>();
        if(lineNumbers == null)
        {
            return null;
        }
        //Sorting the lineNumbers in desceding order
        sortedLineNumbers = characterEditor.insertion_sort(lineNumbers);
        Vector<Integer> finalSortedLineNumbers = new Vector<Integer>();
        finalSortedLineNumbers = characterEditor.removeDuplicates(sortedLineNumbers);
        Scanner scanner = new Scanner(file);
        Vector<String> alphabet = characters.alphabet();
        int lineCounter = 0;
        Vector<String> result = new Vector<String>();
        int i = 1;
        System.out.println("+++++++++++++++++++++++++++++++++");
        System.out.println("+++++++++++++++++++++++++++++++++");
        System.out.println(finalSortedLineNumbers+" -> finalSorted");
        System.out.println(file);
        //Scans through the file to search each line
        while(scanner.hasNext())
        {
            if(i == finalSortedLineNumbers.get(lineCounter))
            {
                String currentLine = scanner.nextLine();
                boolean letterFound = false;
                if(currentLine.contains("\""))
                {
                    result.add(dealWithQuotes(currentLine, oldValue, newValue));
                }
                for(int y = 0; y < alphabet.size(); y++)
                {
                    //Checks to make sure there is no numbers letter before the oldValue
                    if(currentLine.contains("."+oldValue))
                    {
                        if(currentLine.contains(oldValue + alphabet.get(y)))
                        {
                            result.add(dealWithLettersMethod(currentLine, oldValue, newValue));
                            letterFound = true;
                            y = alphabet.size() + 1;
                        }
                        else
                        {
                            letterFound = false;
                            y = alphabet.size() + 1;
                        }
                    }
                    else if(currentLine.contains(alphabet.get(y) + oldValue))
                    {
                        result.add(dealWithLettersMethod(currentLine, oldValue, newValue));
                        letterFound = true;
                        y = alphabet.size() + 1;
                    }
                    else if(currentLine.contains(oldValue + alphabet.get(y)))
                    {
                        //Checks to make sure there is no numbers letter after the oldValue
                        result.add(dealWithLettersMethod(currentLine, oldValue, newValue));
                        letterFound = true;
                        y = alphabet.size() + 1;
                    }
                }
                //If no numbers is found then you may replace the parameter
                if(letterFound == false)
                {
                    if(method)
                    {
                        if(currentLine.contains("."+ oldValue))
                        {
                            result.add(currentLine.replace("."+oldValue, "."+newValue));
                        }
                        else if(currentLine.contains(" "+oldValue+"("))
                        {
                            result.add(currentLine.replace(" "+oldValue+"(", " "+newValue+"("));
                        }
                        else if(currentLine.contains(" "+oldValue+" ("))
                        {
                            result.add(currentLine.replace(" "+oldValue+" (", " "+newValue+" ("));
                        }
                    }
                    else
                    {
                        if(currentLine.contains("."+ oldValue))
                        {
                            result.add(currentLine.replace("."+oldValue, "."+newValue));
                        }
                        else if(currentLine.contains(" "+oldValue))
                        {
                            result.add(currentLine.replace(" "+oldValue, " "+newValue));
                        }
                    }
                }
                if(lineCounter < finalSortedLineNumbers.size())
                {
                    if (lineCounter == finalSortedLineNumbers.size() - 1)
                    {
                    }
                    else
                    {
                        lineCounter++;
                    }
                }
            }
            else
            {
                result.add(scanner.nextLine());
            }
            i++;
        }
        String output = "";
        for(int u = 0; u < result.size(); u++)
        {
            output += result.get(u) + "\n";
        }
        System.out.println("+++++++++++++++++++++++++++++++++");
        System.out.println("+++++++++++++++++++++++++++++++++");
        return output;
    }
    
    /**
     * This method deals with words containing oldValue but with other strings attached to it
     * @param currentLine
     * @param oldValue
     * @param newValue
     * @return
     */
    /*public String dealWithLetters(String currentLine, String oldValue, String newValue)
    {
        StringTokenizer stdot = new StringTokenizer(currentLine,".");
        StringTokenizer st;
        int found3 = 0;
        while(stdot.hasMoreTokens())
        {
            stdot.nextToken();
            found3++;
        }
        if(found3 > 2)
        {
            st = new StringTokenizer(currentLine, ".");
        }
        else
        {
            st = new StringTokenizer(currentLine, " ");
        }
        Vector<String> alphabet = characters.alphabet();
        String result = "";
        String current = "";
        boolean check = false;
        while(st.hasMoreTokens())
        {
            check = false;
            current = st.nextToken();
            System.out.println(current);
            //checks of letters are around oldValue
            if(found3 > 2)
            {
                if(current.contains(oldValue))
                {
                    for(int u = 0; u < alphabet.size(); u++)
                    {
                        if(current.contains((alphabet.get(u) + oldValue)))
                        {
                            result += current + ".";
                            check = true;
                            u = alphabet.size() + 1;
                        }
                        else if(current.contains(oldValue + alphabet.get(u)))
                        {
                            result += current + ".";
                            check = true;
                            u = alphabet.size() + 1;
                        }
                        else
                        {

                        }
                    }
                    //if no letters are found then you may replace
                    if(check == false)
                    {
                        for(int q = 0; q < characters.characterListMethod().size(); q++)
                        {
                                //Checks if the parameter is found with certain characters before and after oldParameter
                            if(current.contains(characters.characterListMethod().get(q) + oldValue))
                            {
                                result += current.replace(characters.characterListMethod().get(q) + oldValue, characters.characterListMethod().get(q) + newValue) + ".";
                            }
                            else
                            {
                                //NOTHING
                            }
                            
                        }
                    }
                }
                else
                {
                    result += current + ".";
                }
            }
            else
            {
                if(current.contains(oldValue))
                {
                    for(int u = 0; u < alphabet.size(); u++)
                    {
                        if(current.contains((alphabet.get(u) + oldValue)))
                        {
                            result += current + " ";
                            check = true;
                            u = alphabet.size() + 1;
                        }
                        else if(current.contains(oldValue + alphabet.get(u)))
                        {
                            result += current + " ";
                            check = true;
                            u = alphabet.size() + 1;
                        }
                        else
                        {

                        }
                    }
                    //if no letters are found then you may replace
                    if(check == false)
                    {
                        System.out.println(current+"+");
                        for(int q = 0; q < characters.characterListMethod().size(); q++)
                        {
                            for(int y = 0; y < characters.characterListMethod().size(); y++)
                            {
                                //Checks if the parameter is found with certain characters before and after oldParameter
                                if(current.contains(characters.characterListMethod().get(q) + oldValue + characters.characterListMethod().get(y)))
                                {
                                    result += current.replace(characters.characterListMethod().get(q) + oldValue + characters.characterListMethod().get(y), characters.characterListMethod().get(q) + newValue + characters.characterListMethod().get(y));
                                }
                                else
                                {
                                    //NOTHING
                                }
                            }
                        }
                    }
                }
                else
                {
                    result += current + " "; 
                }
            }
        }
        //returns refactored string
        return result;
    } */  
    
    /**
     * This method deals with words containing oldValue but with other strings attached to it
     * @param currentLine
     * @param oldValue
     * @param newValue
     * @return
     */
    public String dealWithLettersMethod(String currentLine, String oldValue, String newValue)
    {
        StringTokenizer st = new StringTokenizer(currentLine, " ");
        Vector<String> alphabet = characters.alphabet();
        String result = "";
        String current = "";
        boolean check = false;
        while(st.hasMoreTokens())
        {
            check = false;
            current = st.nextToken();
            System.out.println(current);
            //checks of letters are around oldValue
            if(current.contains(oldValue))
            {
                for(int u = 0; u < alphabet.size(); u++)
                {
                    if(current.contains((alphabet.get(u) + oldValue)))
                    {
                        result += current + ".";
                        check = true;
                        u = alphabet.size() + 1;
                    }
                    else if(current.contains(oldValue + alphabet.get(u)))
                    {
                        result += current + ".";
                        check = true;
                        u = alphabet.size() + 1;
                    }
                    else
                    {

                    }
                }
                //if no letters are found then you may replace
                if(check == false)
                {
                    result += current.replace(oldValue, newValue) + " ";
                }
            }
            else
            {
                result += current + " ";
            }
        }
        //returns refactored string
        return result;
    }   
    
    /*public String dealWithNumbers(String currentLine, String oldValue, String newValue)
    {
        StringTokenizer st = new StringTokenizer(currentLine);
        Vector<Integer> numberList = numbers.Numbers();
        String result = "";
        String current = "";
        boolean check = false;
        while(st.hasMoreTokens())
        {
            check = false;
            current = st.nextToken();
            //checks of letters are around oldValue
            for(int u = 0; u < numberList.size(); u++)
            {
                if(current.contains((numberList.get(u) + oldValue)))
                {
                    result += current + " ";
                    check = true;
                    u = numberList.size() + 1;
                }
                else if(current.contains(oldValue + numberList.get(u)))
                {
                    result += current + " ";
                    check = true;
                    u = numberList.size() + 1;
                }
                else
                {

                }
            }
            //if no letters are found then you may replace
            if(check == false)
            {
                result += current.replace(oldValue, newValue);
            }
        }
        //returns refactored string
        return result;
    }*/

    /**
     * Doesnt change qoutes
     * @param currentLine
     * @return 
     */
    private String dealWithQuotes(String currentLine, String oldValue, String newValue) 
    {
        StringTokenizer st = new StringTokenizer(currentLine);
        String result = "";
        while(st.hasMoreTokens())
        {
            String current = st.nextToken();
            if(current.contains("\""))
            {
                result += current + " ";
            }
            else
            {
                result += current.replace(oldValue, newValue) + " ";
            }
        }
        return result;
    }

    
    /**
     * Deals with oldParameter which is surrounded by any extra characters such as numbers or alphabet
     * @param currentLine
     * @param oldParameter
     * @param newParameter
     * @return 
     */
    public String dealWithExtras(String currentLine, String oldParameter, String newParameter) 
    {
        StringTokenizer st = new StringTokenizer(currentLine, " ");
        String result = "";
        while(st.hasMoreTokens())
        {
            boolean check = false;
            String current  = st.nextToken();
            if(current.contains(oldParameter))
            {
                for(int u = 0; u < characters.alphabet().size(); u++)
                {
                    if(current.contains(characters.alphabet().get(u) + oldParameter))
                    {
                        result += current + " ";
                        check = true;
                        u = characters.alphabet().size();
                    }
                    else if(current.contains(oldParameter +characters.alphabet().get(u)))
                    {
                        result += current + " ";
                        check = true;
                        u = characters.alphabet().size();
                    }
                }
                if(check == false)
                {
                    result += current.replace(oldParameter, newParameter) + " ";
                }
            }
            else
            {
                result += current + " ";
            }
        }
        return result;
    }
}
